package thread;

import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yinlei
 * @date 2018/7/20
 * @since 1.0.0
 */
public class ProducerConsumerMain {

    static final int max = 10;

    public static void main(String[] args) {

        Pcm sync = new BlockQueue();
        new Thread(()-> sync.consume()).start();
//        new Thread(()-> sync.consume()).start();
//        new Thread(()-> sync.consume()).start();
        new Thread(()-> sync.produce()).start();
        new Thread(()-> sync.produce()).start();
        new Thread(()-> sync.produce()).start();
        new Thread(()-> sync.produce()).start();
        new Thread(()-> sync.produce()).start();
//        new Thread(()-> sync.produce()).start();
//        new Thread(()-> sync.produce()).start();
//        new Thread(()-> sync.produce()).start();
//        new Thread(()-> sync.produce()).start();
//        new Thread(()-> sync.consume()).start();
        new Thread(()-> sync.consume()).start();
        new Thread(()-> sync.consume()).start();
        new Thread(()-> sync.consume()).start();
        new Thread(()-> sync.consume()).start();

    }

    interface Pcm {

        void produce();
        void consume();
    }

    public static class BlockQueue implements Pcm {

        LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>(max);

        @Override
        public void produce() {
            try {
                long id = Thread.currentThread().getId();
                queue.put(id);
                System.out.println(id + "---");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void consume() {

            try {
                System.out.println(queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class RLock implements Pcm {
        ReentrantLock lock = new ReentrantLock();
        LinkedList<Object> store = new LinkedList<>();

        Condition full = lock.newCondition();
        Condition empty = lock.newCondition();

        public void produce() {

            try {
                lock.lock();
                while (store.size() > max) {
                    try {
                        full.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                store.add(Thread.currentThread().getId());

                System.out.println("put " + Thread.currentThread().getId());

                empty.signalAll();
            } finally {
                lock.unlock();
            }
        }

        public void consume() {

            try {
                lock.lock();
                while (store.size() < 1) {
                    try {
                        empty.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println(store.remove());

                full.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }

    public static class Sync implements Pcm {
        LinkedList<String> store = new LinkedList<>();

        public void produce() {

            try {
                synchronized (store) {
                    while (store.size() > max) {
                        store.wait();
                    }
                    store.add(Thread.currentThread().getId() + "");
                    System.out.println(Thread.currentThread().getId() + "put");
                    store.notifyAll();
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

        }

        public void consume() {
            try {
                synchronized (store) {
                    while (store.size()  < 1) {
                        store.wait();
                        System.out.println("zuse");
                    }
                    System.out.println(store.remove());

                    store.notifyAll();
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

}
